package cn.iocoder.idata.module.idata.controller.admin.batchTask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.batchTask.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.batchTask.BatchTaskDO;
import cn.iocoder.idata.module.idata.convert.batchTask.BatchTaskConvert;
import cn.iocoder.idata.module.idata.service.batchTask.BatchTaskService;

@Tag(name = "管理后台 - 批次任务")
@RestController
@RequestMapping("/idata/batch-task")
@Validated
public class BatchTaskController {

    @Resource
    private BatchTaskService batchTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建批次任务")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:create')")
    public CommonResult<Long> createBatchTask(@Valid @RequestBody BatchTaskCreateReqVO createReqVO) {
        return success(batchTaskService.createBatchTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新批次任务")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:update')")
    public CommonResult<Boolean> updateBatchTask(@Valid @RequestBody BatchTaskUpdateReqVO updateReqVO) {
        batchTaskService.updateBatchTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除批次任务")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:delete')")
    public CommonResult<Boolean> deleteBatchTask(@RequestParam("id") Long id) {
        batchTaskService.deleteBatchTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得批次任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:query')")
    public CommonResult<BatchTaskRespVO> getBatchTask(@RequestParam("id") Long id) {
        BatchTaskDO batchTask = batchTaskService.getBatchTask(id);
        return success(BatchTaskConvert.INSTANCE.convert(batchTask));
    }

    @GetMapping("/list")
    @Operation(summary = "获得批次任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:query')")
    public CommonResult<List<BatchTaskRespVO>> getBatchTaskList(@RequestParam("ids") Collection<Long> ids) {
        List<BatchTaskDO> list = batchTaskService.getBatchTaskList(ids);
        return success(BatchTaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得批次任务分页")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:query')")
    public CommonResult<PageResult<BatchTaskRespVO>> getBatchTaskPage(@Valid BatchTaskPageReqVO pageVO) {
        PageResult<BatchTaskDO> pageResult = batchTaskService.getBatchTaskPage(pageVO);
        return success(BatchTaskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出批次任务 Excel")
    // @PreAuthorize("@ss.hasPermission('idata:batch-task:export')")
    @OperateLog(type = EXPORT)
    public void exportBatchTaskExcel(@Valid BatchTaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BatchTaskDO> list = batchTaskService.getBatchTaskList(exportReqVO);
        // 导出 Excel
        List<BatchTaskExcelVO> datas = BatchTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "批次任务.xls", "数据", BatchTaskExcelVO.class, datas);
    }

}
